package cn.szzsi._web;

import cn.szzsi.common.model.FavoriteProduct;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.MemberProduct;
import cn.szzsi.common.model.Product;
import cn.szzsi.service.FavoriteProductService;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

import java.util.List;

/**
 * 前台类 - 收藏夹
 * 
 */
public class FavoriteController extends BaseWebController<FavoriteProduct> {
	
	private static final Integer pageSize = 10;// 商品收藏每页显示数
	private Product product;
	private MemberProduct memberProduct;
	private String path;//模板路径
	private String shopId;//用户的shopId就是商户的id
	
	// 商品收藏列表
	public void list(){
		shopId=getLoginMember().getStr("shopId");//用户的shopId对应商户的id(uid),商户的shopId为"-1"
		Member loginMember = getLoginMember();
		System.out.println(FavoriteProduct.dao.getFavoriteProductList(loginMember.getId()));
		if(loginMember!=null){
			setAttr("favoriteProducts",FavoriteProduct.dao.getFavoriteProductList(loginMember.getId()));
		}
		wapTemplatePath("favorite_list.html");
	}

	//添加收藏商品
	@Clear
	public void addFavorite(){
		Member loginMember = getLoginMember();
		String productId=getPara("productId");//商品id
		if(loginMember==null){
			renderJson(Ret.fail("info","您还没有登陆,暂无法做此操作!"));
			return;
		}
		FavoriteProduct favoriteProduct=FavoriteProduct.dao.getByMemberIdAndProductId(loginMember.getId(),productId);
		if(favoriteProduct!=null){
			renderJson(Ret.fail("info","您已收藏过此商品,无需再次收藏!"));
			return;
		}
		FavoriteProductService.service.saveInfo(loginMember,productId);
		renderJson(Ret.ok());

	}


	/**
	 * 删除收藏商品
	 */
	public void deleteFavorite(){
		Member loginMember = getLoginMember();
		String productId=getPara("productId");//商品id
		FavoriteProductService.service.deleteInfo(loginMember,productId);
		renderJson(Ret.ok());

	}

	// 添加收藏商品
	/*@Clear
	public void ajaxAdd() {
		id = getPara("id","");		
		if(StrKit.isBlank(id)){
			addActionError("此商品ID不能为空!");
			return;
		}			
		product = Product.dao.findById(id);
		if (!product.getBoolean("isMarketable")) {
			ajaxJsonErrorMessage("此商品已下架!");
			return;
		}
		Member loginMember = getLoginMember();
		
		List<Product> favoriteProductList = loginMember.getFavoriteProductList();
		
		if (favoriteProductList.size() > 0 && favoriteProductList.contains(product)) {
			ajaxJsonWarnMessage("您已经收藏过此商品!");
		} else {
			memberProduct = new MemberProduct();
			memberProduct.set("favoriteMemberSet_id", loginMember.getStr("id"));
			memberProduct.set("favoriteProductSet_id", product.getStr("id"));
			memberProduct.save();
			ajaxJsonSuccessMessage("商品收藏成功!");
		}
	}*/
		
	// 删除收藏商品
	/*public void delete () {
		id = getPara("id","");
		String loginMember = getLoginMember().getStr("id");
		String sql = "delete from mall_member_product where favoriteMemberSet_id = ? and favoriteProductSet_id = ?";
		if (StrKit.notBlank(id)){
			int row = Db.update(sql,loginMember,id);
			if (0 < row){
				redirect("/b/favorite/list");
			} else{
				ajaxJsonErrorMessage("删除失败!");
			}
		}
		
	}*/
}
